home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / l33t_liek_jeff_k / leet source / HQApplication.m < prev    next >
Text File  |  2000-06-23  |  6KB  |  138 lines

  1. //
  2. //  HQApplication.m
  3. //  HackMac
  4. //
  5.  
  6. #import "HQApplication.h"
  7. #import <objc/objc-class.h>  // We need this in order to dereference a Class
  8.  
  9. extern BOOL gOn;
  10.  
  11. @implementation HQApplication
  12.  
  13. + (void)doNSAppInstancePose {
  14.     if (NSApp) {
  15.         // Swizzle our class object (and its meta-class) to be a subclass of the class that NSApp is a member of.  This could be a real stupid thing to do if any instances were going to be created and our new superclass had ivars.  But this situation is fairly controlled and no more instances of NSApplication subclasses should ever be created.
  16.         Class realNSAppClass = ((HQApplication *)NSApp)->isa;
  17.         Class hqAppClass = [HQApplication class];
  18.         Class realNSAppMetaClass = ((HQApplication *)realNSAppClass)->isa;
  19.         Class hqAppMetaClass = ((HQApplication *)hqAppClass)->isa;
  20.         
  21.         hqAppClass->super_class = realNSAppClass;
  22.         hqAppMetaClass->super_class = realNSAppMetaClass;
  23.         
  24.         // Swizzle NSApp into an instance of our subclass.
  25.         // We cast to HQApplication as a simple way to get around the fact that isa is @protected.  It does not matter what we cast to, really, since everything has an isa.
  26.         ((HQApplication *)NSApp)->isa = self;
  27.     } else {
  28.         NSLog(@"NSApp does not exist yet!");
  29.     }
  30. }
  31.  
  32. - (void)sendEvent:(NSEvent *)event {
  33.     static unichar lastchar = 0, lasterchar = 0;
  34.     unichar thischar;
  35.     unichar from[] = {'a', 'e', 'i', 'o', 's', 't'};
  36.     unichar   to[] = {'4', '3', '1', '0', '5', '7'};
  37.     int skin;
  38.  
  39.     if ( [event type] == NSKeyDown && gOn ) {
  40.         thischar = [[event characters] characterAtIndex:0];
  41.         thischar = tolower(thischar);
  42.  
  43.         if( lastchar == 'c' && thischar == 'k' ) {
  44.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  45.             [ self keypress:event withChar:'x' ];
  46.         } else if( thischar == 'f' ) {
  47.             [ self keypress:event withChar:'p' ];
  48.             [ self keypress:event withChar:'h' ];
  49.         } else if( lastchar == 'p' && thischar == 'h' ) {
  50.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  51.             [ self keypress:event withChar:'f' ];
  52.         } else if( lastchar == 'o' && thischar == 'r' ) {
  53.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  54.             [ self keypress:event withChar:'r' ];
  55.             [ self keypress:event withChar:'0' ];
  56.         } else if( lasterchar == 'e' && lastchar == 'r' && thischar == ' ' ) {
  57.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  58.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  59.             [ self keypress:event withChar:'0' ];
  60.             [ self keypress:event withChar:'r' ];
  61.             [ self keypress:event withChar:' ' ];
  62.         } else if( lasterchar == 'a' && lastchar == 'n' && thischar == 'y' ) {
  63.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  64.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  65.             [ self keypress:event withChar:'n' ];
  66.             [ self keypress:event withChar:'e' ];
  67.         } else if( lasterchar == 'o' && lastchar == 'n' && thischar == 'e' ) {
  68.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  69.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  70.             [ self keypress:event withChar:'1' ];
  71.         } else if( lasterchar == 'y' && lastchar == 'o'&& thischar == 'u') {
  72.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  73.             [ self keypress_special:event withChar:NSDeleteCharacter ];
  74.             [ self keypress:event withChar:'j' ];
  75.             [ self keypress:event withChar:'0' ];
  76.             [ self keypress:event withChar:'0' ];
  77.         } else {
  78.             for( skin = 0; skin < 6; skin++ ) {
  79.                 if( thischar == from[skin] ) break;
  80.             }
  81.             if( skin < 6 && ( rand() & 0x10000000 ) ) {
  82.                 [ self keypress:event withChar:to[skin] ];
  83.             } else {
  84.                 [ self keypress:event withChar:thischar ];
  85.             }
  86.         }
  87.         lasterchar = lastchar;
  88.         lastchar = thischar;
  89.     } else {
  90.         [super sendEvent:event];
  91.     }
  92. }
  93.  
  94. - (void)keypress :(NSEvent *)event withChar:(unichar)thechar {
  95.     NSString *mystring;
  96.  
  97.     if( !( rand() & 0x11000000 ) ) {
  98.         thechar = toupper(thechar);
  99.     }
  100.     mystring = [NSString stringWithCharacters:&thechar length:1];
  101.  
  102.     [super
  103.     sendEvent:[NSEvent
  104.             keyEventWithType:[event type]
  105.             location:[event locationInWindow]
  106.             modifierFlags:[event modifierFlags]
  107.             timestamp:[event timestamp]
  108.             windowNumber:[event windowNumber]
  109.             context:[event context]
  110.             characters:mystring
  111.             charactersIgnoringModifiers:[event charactersIgnoringModifiers]
  112.             isARepeat:[event isARepeat]
  113.             keyCode:[event keyCode]
  114.         ]
  115.     ];
  116. }
  117.  
  118. - (void)keypress_special :(NSEvent *)event withChar:(unichar)thechar {
  119.     NSString *mystring = [NSString stringWithCharacters:&thechar length:1];
  120.  
  121.     [super
  122.     sendEvent:[NSEvent
  123.             keyEventWithType:[event type]
  124.             location:[event locationInWindow]
  125.             modifierFlags:[event modifierFlags]
  126.             timestamp:[event timestamp]
  127.             windowNumber:[event windowNumber]
  128.             context:[event context]
  129.             characters:mystring
  130.             charactersIgnoringModifiers:mystring
  131.             isARepeat:[event isARepeat]
  132.             keyCode:[event keyCode]
  133.         ]
  134.     ];
  135. }
  136.  
  137. @end
  138.